/**
 * package for all of the controller classes to fit MVC design pattern
 */
package javamonsters.controller; // $codepro.audit.disable packagePrefixNamingConvention
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javamonsters.model.SolarSystem;
import javamonsters.universe.application.UniverseController;
import javamonsters.view.MainPanel;
import javamonsters.view.SpaceTraders;
import javamonsters.view.TitleScreenView;

import javax.swing.JOptionPane;



/**
 * TitleScreenViewController controls TitleScreenView, 
 * handling the actions that occur on the title screen
 * 
 * @author elahurd3
 * @version 1.0
 *
 */
public class TitleScreenViewController {
	
	/**
	 * the title screen view
	 */
	private final TitleScreenView main;
	
	/**
	 * main game controller
	 */
	private final SpaceTraders parentController;
	
	/**
	 * the main panel
	 */
	private final MainPanel card;
	
	/**
	 * TitleScreenViewController constructor
	 * 
	 * @param main the view for this controller
	 * @param card the main panel that handles switching between all of the views
	 * @param parentController the main game controller
	 * 
	 */
	public TitleScreenViewController(
		TitleScreenView main, final MainPanel card, SpaceTraders parentController) {
		this.main = main;
		this.card = card;
		this.parentController = parentController;

		main.getExitButton().addActionListener(new ExitButtonListener());
		main.getNewButton().addActionListener(new NewButtonListener());
		main.getLoadButton().addActionListener(new LoadButtonListener());
	}
	
	/**
	 * @return String representation of title screen view controller
	 */
	public String toString() {
		return "title screen view controller";
	}
	
	/**
	 * @return the title screen view for this controller
	 */
	public TitleScreenView getTitleScreenView() {
		return main;
	}

	/**
	 * Button listener for the exit button on the title screen
	 * @author elahurd3
	 *
	 */
	private static class ExitButtonListener implements ActionListener {
		/**
		 * exits game when Exit Game button is pressed
		 * @param e ActionEvent that represents the press of a button
		 */
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
		
		/**
		 * @return String representation of exit button listener
		 */
		public String toString() {
			return "exit button listener";
		}
		
	}
	
	/**
	 * Button listener to detect when a new game is desired
	 * @author evan
	 */
	private class NewButtonListener implements ActionListener {
		/**
		 * opens configuration screen when New Game button is pressed
		 * @param e ActionEvent that represents the press of a button
		 */
		public void actionPerformed(ActionEvent e) {
			card.getLayout().show(card, "Configuration Screen");
			final String message = "Welcome to Space Traders!  " +
					         "Enter your name and difficulty, " +
							 "and allot your 16 points among the 4 " +
							 "skill types.  Good luck, pilot!";
			JOptionPane.showMessageDialog(null, message);
		}
		
		/**
		 * @return String representation of new button listener
		 */
		public String toString() {
			return "new button listener";
		}
		
	}
	
	/**
	 * button listener that handles loading the game
	 * 
	 * @author wa
	 * @version 1.0
	 */
	private class LoadButtonListener implements ActionListener {
		/**
		 *@param e ActionEvent that represents the press of a button
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				if (parentController != null) {
					if (parentController.canLoadGame()) { // $codepro.audit.disable nullPointerDereference
						final SolarSystem system = parentController.getCurrSystem();
						final UniverseController universeController = 
								parentController.getUniverseController();
						
						universeController
						.getSideView().getSystemLabel().setText(system.getName());
						
						universeController.getSideView()
						.getTechLevelLabel().setText(system.getTechLevel().toString());
						
						final int fuel = parentController.getPlayer().getShip().getFuel();
						
						universeController.getSideView()
						.getCurrentFuelLabel().setText("" + fuel);
						
						universeController
						.getSideView().getFuelCostLabel().setText("" + 0);
						
						System.out.println(parentController.getPlayer());
						
						card.getLayout().show(card, "Dual Universe Panel");
					}
				}
				
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		/**
		 * @return String representation of load button listener
		 */
		@Override
		public String toString() {
			return "load button listener";
		}
	}

}
